from flask import request
from exts import db, api
from flask_restful import Resource
from utils.auth import auth_service


class AdminLogin(Resource):
    def post(self):
        try:
            post_json = request.get_json()
            if not post_json:
                return {"message": "参数错误"}, 400
            result = auth_service().login(post_json)
            if isinstance(result, tuple):  # 处理错误情况
                return result
                
            return {
                "message": "登录成功",
                "data": result,
                "code": 200
            }
        except Exception as e:
            return {"message": str(e)}, 400
        finally:
            db.session.close()

class AdminLogout(Resource):
    @auth_service().verify_token
    def post(self):
        try:
            return auth_service().logout()
        except Exception as e:
            return {"message": str(e)}, 400
        finally:
            db.session.close()


api.add_resource(AdminLogin, "/admin/login")
api.add_resource(AdminLogout, "/admin/logout")